package com.ntschy.taizhou.service1;

import com.ntschy.taizhou.dao.HelloListDao;
import com.ntschy.taizhou.entity.TestUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Service
public class UserTansService2 {

    @Autowired
    HelloListDao helloListDao;

 // @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRES_NEW)
    public Object insertUser2(TestUser testUser) {
        System.out.println("UserTansService2---,1--");

       // helloListDao.insertUser(testUser);
        //int a = 1 / 0;
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                System.out.println("afterCommit-------");
                System.out.println("UserTansService2-----" + TransactionSynchronizationManager.getCurrentTransactionName());
                System.out.println("UserTansService2----" + TransactionSynchronizationManager.getCurrentTransactionIsolationLevel());
                // int a=1/0;
              //  System.out.println(testUser.toString());
                testUser.setName(testUser.getName()+"--2");
                helloListDao.insertUser(testUser);
               // int a = 1 / 0;
            }

            // 事务提交之前
            @Override
            public void beforeCommit(boolean readOnly){
                System.out.println("beforeCommit-------");
            }

            // 事务成功或者事务回滚之前
            @Override
            public void beforeCompletion(){
                System.out.println("beforeCompletion-------");
            }


            // 操作完成之后(包含事务成功或者事务回滚)
            @Override
            public void afterCompletion(int status){
                System.out.println("afterCompletion-------");
            }
        });

      System.out.println("UserTansService2---,2--");
        return "新增成功";
    }

}
